#!/bin/bash

# 执行cmake编译等等的操作
do_cmake()
{
	if [ -d build ]; then # 判断文件夹是否已经存在
		cd build # 进入build文件夹
		echo make[ 2 ] : Entering directory $PWD

		echo There are some leftover files that we are going to delete it: 
		for file1111 in `ls `; do 
			echo  " " $file1111  # 输出文件/夹
		done
		rm -rf * # 删除当前的所有文件
	else
		mkdir build && cd build # 创建并进入文件夹
	fi

    cmake -G"Unix Makefiles" ../ # cmake创建的指令
    make # make生成可执行文件
	# 判断当前的make命令是否执行成功, $?用于采集上调命令的执行的返回值
	if [ $? -ne 0 ]; then
        echo 
		echo [ error! ] make failed!
		echo make[ 2 ] : Leaving directory $PWD
		exit -1
    else
        echo [ Congratulations! ] make successed!
		echo make[ 2 ] : Leaving directory $PWD
    fi
	
	cd ..
	echo make[ 2 ] : Leaving directory $PWD
}

# 遍历当前目录下的文件(夹)
do_check_sub_dir()
{
	for file1 in `ls `; do
		if [  -d $file1 ]; then
			cd $file1
			echo 
			echo make[ 1 ] : Entering directory $PWD
			
			do_cmake $file1

			cd ..
			echo make[ 1 ] : Leaving directory $PWD
		fi
	done
}

for file in `ls $1`; do
	echo
	if [  -d $file ]; then
		echo $file is dir
		cd $file
		# 遍历当前目录下的文件(夹)
		do_check_sub_dir 
		cd ..
	else
		echo $file not dir
	fi
	# 调试的时候方便查看打印信息
	# sleep 1
done
